package com.java.business.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.business.service.CourseService;
import com.java.common.utils.PageUtils;
import com.java.common.utils.Query;
import com.java.business.dao.SectionDao;
import com.java.business.entity.Section;
import com.java.business.service.SectionService;
import com.java.common.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service("sectionService")
public class SectionServiceImpl extends ServiceImpl<SectionDao, Section> implements SectionService {
    @Autowired
    private CourseService courseService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Section> page = this.page(
                new Query<Section>().getPage(params),
                // 拼接查询条件
                new LambdaQueryWrapper<Section>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void updateOrSave(Section section) {
        if (StrUtil.isNotBlank(section.getId())) {
            this.updateById(section);
        } else {
            section.setId(UuidUtil.getShortUuid());
            this.save(section);
        }
        // 更新总时长
        courseService.updateTime(section.getCourseId());
    }

}